package com.fengwk.cv4j.view.ind;

import org.nd4j.linalg.api.ndarray.INDArray;

import com.fengwk.cv4j.compute.Compute;
import com.fengwk.cv4j.factory.ViewFactory;
import com.fengwk.cv4j.view.View;
import com.fengwk.cv4j.view.common.SumView;
import com.fengwk.support.exception.BaseException;

/**
 * 矩阵求和
 * 
 * @author fengwk
 *
 */
public class INDSumView extends SumView<INDArrayWrap> {

	private static final long serialVersionUID = -1826574298429385659L;

	public INDSumView(Compute<INDArrayWrap> compute, ViewFactory<INDArrayWrap> factory, @SuppressWarnings("unchecked") View<INDArrayWrap>...vs) {
		super(compute, factory, vs);
		checkVsMin(2, vs);
	}
	
	@Override
	public View<INDArrayWrap> gradient0(int idx) {
	    INDArrayWrap wrap0 = t(0);
	    INDArray arr = wrap0.arr();
	    for (int i = 1; i < len(); i ++) {
            Number num = t(i).num();
            if (num == null)
                throw new BaseException("num == null");
        }
	    if (arr == null) {
	        return super.gradient0(idx);
	    } else {
	        if (idx == 0)
	            return f().of(1, wrap0);
	        if (idx < len())
	            return f().of(0);
	        throw new BaseException("idx >= len()"); 
	    }
	}
	
}
